﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CNFProblem
{
    /// <summary>
    /// Interaction logic for OneLiteral.xaml
    /// </summary>
    public partial class OneLiteral : UserControl
    {
        public event EventHandler Remove;

        private int UsedSlots
        {
            get
            {
                if (string.IsNullOrEmpty(_v1.Text)) return 0;
                if (string.IsNullOrEmpty(_v2.Text)) return 1;
                if (string.IsNullOrEmpty(_v3.Text)) return 2;
                return 3;
            }
        }

        public void SetVariables( List<CNF.VariableRepresentation> variables )
        {
            if (variables.Count > 3 || variables.Count < 0) throw new ArgumentOutOfRangeException();
            if (variables.Count > 0)
            {
                _v1.Text = variables[0].Name;
                _n1.Tag = variables[0].IsNegated ? "1" : "0";
            }
            if (variables.Count > 1)
            {
                _v2.Text = variables[1].Name;
                _n2.Tag = variables[1].IsNegated ? "1" : "0";
            }
            if (variables.Count > 2)
            {
                _v3.Text = variables[2].Name;
                _n3.Tag = variables[2].IsNegated ? "1" : "0";
            }
        }

        public string LiteralAlternativeStr
        {
            get
            {
                int slots = UsedSlots;
                if (slots == 0) return null;

                string la = "";
                if (((string)_n1.Tag) != "0")
                    la += CNF.Parser.negation;

                la += _v1.Text;

                if (slots >= 2)
                {
                    la += " ";
                    if (((string)_n2.Tag) != "0")
                        la += CNF.Parser.negation;

                    la += _v2.Text;
                }

                if (slots >= 3)
                {
                    la += " ";
                    if (((string)_n3.Tag) != "0")
                        la += CNF.Parser.negation;

                    la += _v3.Text;
                }

                return la;
            }
        }

        public OneLiteral()
        {
            InitializeComponent();
        }

        private void _n1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (((string)_n1.Tag) != "0")
                _n1.Tag = "0";
            else
                _n1.Tag = "1";
        }

        private void _n2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (((string)_n2.Tag) != "0")
                _n2.Tag = "0";
            else
                _n2.Tag = "1";
        }

        private void _n3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (((string)_n3.Tag) != "0")
                _n3.Tag = "0";
            else
                _n3.Tag = "1";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Remove(this, new EventArgs());
        }
    }
}
